<?php



namespace app\api\controller\pifa;



use app\common\controller\Api;

use think\Config;

use think\Db;

use think\Validate;

use think\Session;

use fast\WechatMini;

use addons\third\model\Third;

use think\Log;

use think\Request;

use fast\Http;

use think\Exception;

use addons\third\library\Service;
use fast\Huipaynew;


/**

 * 首页接口

 */

class User extends Api

{

    protected $noNeedLogin = ['login','getconfig','getMendian1','token','laobaninit','taocanlist'];

    protected $noNeedRight = ['*'];





    public function _initialize()

    {

        $this->token = $this->request->post('token');



        parent::_initialize();



    } 


    public function search_kehu(){

        $post = $this->request->request();





        $map['xiaoshou_id'] = $this->auth->id;
        $map['user_id'] = ['<>',0];
        $list = Db::table('fa_pifa_kehu')->where($map)->where(function ($query) use ($post) {
                        if (!empty($post['keyword'])) {
                            $query->whereOr('phone','like','%'.$post['keyword'].'%');
                            $query->whereOr('consignee','like','%'.$post['keyword'].'%');
                            $query->whereOr('md_name','like','%'.$post['keyword'].'%');
                        } 
                       
                    })->select();



        $this->success("成功", $list);

    }



    public function orderinit(){

        $post = $this->request->request();





        $map['user_id'] = $this->auth->id;





        $order = Db::table('fa_pifa_order')->field('COUNT(*) as num,orderstate')->where($map)->group('orderstate')->select();







        $order_num[0] = 0;

        $order_num[1] = 0;

        $order_num[2] = 0;

        $order_num[3] = 0;

        $order_num[4] = 0;

        $order_num[5] = 0;



        foreach ($order as $key => $value) {

            $order_num[$value['orderstate']] = $value['num'];

        }





        $this->success("成功", $order_num);

    }

    //身份状态

    public function shenfeninit()
    {  

    
        $map['user_id'] = $this->auth->id;

        $map['status'] = '1';

        $list[0] = 0;

        $list[1] = 0;

        $result = Db::table('fa_pifa_tuiguangyuan')->where($map)->find();

        if (!empty($result)) {

           $list[0] = 1;

        }
        $result = Db::table('fa_pifa_daili')->where($map)->find();

        if (!empty($result)) {

           $list[1] = 1;

        }
        $this->success("成功", $list);

    }



    //用户收藏

    public function getScList(){

        $post = $this->request->request();





        $Sc = new \app\admin\model\pifa\Sc;



        $map['user_id'] = $this->auth->id;

        $list = $Sc->with(['goods'])->where($map)->paginate(12);

        foreach ($list as $row) {

                

            $row->getRelation('goods')->visible(['goods_name','image','desc']);

        }

        $this->success("成功", $list);

    }

    public function getSc(){

        $post = $this->request->request();
        
        $map['user_id'] = $this->auth->id;
        $map['goods_id'] = $post['goods_id'];
        $list = Db::table('fa_pifa_sc')->where($map)->find();

        if (!empty($list)) {
            $this->success("成功", 1);
        }else{
            $this->success("成功", 0);
        }


    }


    public function chosesc(){

        $post = $this->request->request();
        
        $map['user_id'] = $this->auth->id;
        $map['goods_id'] = $post['goods_id'];
        $list = Db::table('fa_pifa_sc')->where($map)->find();

        if (empty($list)) {


            $map['createtime'] = time();
            $map['updatetime'] = time();
            Db::table('fa_pifa_sc')->insert($map);


            $this->success("成功", 1);
        }else{
            Db::table('fa_pifa_sc')->where($map)->delete();
            $this->success("成功", 0);
        }


    }


    public function chongzhi(){

        $post = $this->request->request();

        $info = Db::table('fa_pifa_taocan')->find($post['id']);

        if (empty($info)) {
           $this->error("套餐不存在");
        }

                $paytype = 'wechat';
                $method = 'miniapp';
                $third = Third::where('platform', 'wechat')->where('apptype', 'miniapp')->where('user_id', $this->auth->id)->find();
                
                if (!$third) {
                    $this->error("未找到登录用户信息");
                }
                $openid = $third['openid'];

                $indata2['order_no'] = 'CZ'.date("YmdHis"). $this->auth->id. mt_rand(100, 999);

                $indata2['price'] = $info['price'];
                $indata2['send_price'] = $info['send'];
                
                $indata2['createtime'] = time();
                Db::table('fa_pifa_ordertaocan')->insertGetId($indata2);    
                $orderid = $indata2['order_no'];
                $paytype = 'wechat';
                $notifyurl = 'http://dongpingyun.xinanqy.com/api/paynew/chongzhi';
               
                $ip = request()->ip();

             
                try {
                    
                    $res = Huipaynew::addOrder($openid,$indata2['order_no'],$info['price'],$notifyurl,$ip);

                } catch (Exception $e) {

                    $this->error($e->getMessage());

                }

                var_dump($res);
              
                if (!$res['success']) {
                    $this->error("请求支付失败");
                }
                
                

                   //如果是Collection则需要转换为数组

                $result = json_decode($res['result'],true);
                
                
                if($result['orderStatus'] == 'FAIL'){
                      $this->error("请求支付失败");
                }
                
                
                logs('third_pay',json_encode($result,JSON_UNESCAPED_UNICODE));  

                $qrCode = json_decode($result['qrCode'],true);
                // $qrCode['signData'] = json_decode($qrCode['signData'],true);

                // $qrCode['amount']['currency'] = 'CNY';
                $resdata['pay'] = $qrCode;
                // $resdata['pay'] = json_decode($resdata['pay'],true);

              
               
                $resdata['price'] = floatval($order['price']);
                $resdata['order_no'] = $indata2['order_no'];
                $this->success("请求成功", $resdata);

        $this->success("成功", $list);
    }
    

     public function taocanlist(){

        $post = $this->request->request();



        $map['status'] = '1';
        $list = Db::table('fa_pifa_taocan')->order('weigh asc')->select();
        $this->success("成功", $list);
    }



    public function getShangjia(){

        $post = $this->request->request();



        $shangjia = Db::table('fa_pifa_shangjia')->find($post['id']);



        if (!empty($shangjia)) {

            $this->success("成功", $shangjia);

        }else{

            $this->error("商家异常");

        }

    }

     





      public function userInit()

    {  

        $map['inv_id'] = $this->auth->id;

        $map['orderstate'] = ['in',[0,1,2,3]];

   

        $result['z_fan'] = Db::table('fa_pifa_order')->where($map)->sum('fen_price');

        $map['orderstate'] = ['in',[0,1,2]];;

        $result['z_dongjie'] = Db::table('fa_pifa_order')->where($map)->sum('fen_price');     

        $this->success("成功", $result);

    }

  





    public function xiaoshouinit()

    {  

        $map['inv_id'] = $this->auth->id;

        $map['orderstate'] = ['in',[1,2,3]];

        $map['createday'] = ['BETWEEN',[date('Y-m-01'),date('Y-m-31')]];

   

        $result['order_num_month'] = Db::table('fa_pifa_order')->where($map)->count();

    

        $result['order_price_month'] = Db::table('fa_pifa_order')->where($map)->sum('price');



        $map['createday'] = ['BETWEEN',[date('Y-01-01'),date('Y-12-31')]];

        $result['order_num_year'] = Db::table('fa_pifa_order')->where($map)->count();

        $result['order_price_year'] = Db::table('fa_pifa_order')->where($map)->sum('price');



        $map2['xiaoshou_id'] = $this->auth->id;

        $map2['user_id'] = ['<>',0];

        $result['kehu'] = Db::table('fa_pifa_kehu')->where($map2)->count();





        $this->success("成功", $result);

    }



     public function qudaoinit()

    {  







        $result['leiji'] = 0;

        $result['tixian_leiji'] = 0;

        $result['dongjie'] = 0;







        $map['inv_id'] = $this->auth->id;

        $map2['fenstate'] = 1;

        $result['leiji'] = Db::table('fa_pifa_order')->where($map2)->sum('fen_price');

        $map2['fenstate'] = 0;

        $result['dongjie'] = Db::table('fa_pifa_order')->where($map2)->sum('fen_price');





        $map3['user_id'] = $this->auth->id;

        $map3['status'] = 'successed';    



        $result['tixian_leiji'] = Db::table('fa_withdraw')->where($map3)->sum('money');









        $map['inv_id'] = $this->auth->id;

        $map['orderstate'] = ['in',[1,2,3]];

        $map['createday'] = ['BETWEEN',[date('Y-m-01'),date('Y-m-31')]];

   

        $result['order_num_month'] = Db::table('fa_pifa_order')->where($map)->count();

    

        $result['order_price_month'] = Db::table('fa_pifa_order')->where($map)->sum('price');



        $map['createday'] = ['BETWEEN',[date('Y-01-01'),date('Y-12-31')]];

        $result['order_num_year'] = Db::table('fa_pifa_order')->where($map)->count();

        $result['order_price_year'] = Db::table('fa_pifa_order')->where($map)->sum('price');



        





        $this->success("成功", $result);

    }



    //发起提现

    public function tixian()

    {

        $post = $this->request->request();





        $site = Config::get("site");



        if ($site['tixian'] == 1) {

             if (empty($post['account'])) {

                $this->error("请输入支付宝账户");

            }



            if (empty($post['name'])) {

                $this->error("请输入支付宝姓名");

            }

        }



       



        $user = Db::table('fa_user')->find($this->auth->id);



        if ($user['money'] < $post['money']) {

            $this->error("余额不足无法提现");

        }





         if ($site['tixian'] != 1) {

            $this->error("提现维护中");

        }



        $indata['user_id'] = $this->auth->id;

        $indata['money'] = $post['money'];

        $indata['handingfee'] = 0;

        $indata['taxes'] = 0;







        $indata['account'] = $post['account'];

        $indata['name'] = $post['name'];

        $indata['orderid'] = 'TX'.date("YmdHis"). $this->auth->id. mt_rand(100, 999);

        $indata['createtime'] = time();

        $indata['updatetime'] = time();

        $indata['type'] = '支付宝';



         \app\common\model\User::money(-$post['money'], $this->auth->id, '申请提现');

        Db::table('fa_withdraw')->insert($indata);

        $this->success("提现成功");



    }



   



    public function noticelist()

    {

      $map['user_id'] = ['in',[0,$this->auth->id]];



      $list = Db::table('fa_pifa_notice')->where($map)->order('createtime desc')->paginate(12);

      $list = $list->toArray();

      foreach ($list['data'] as $key => $value) {

         $list['data'][$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);

      }



      $this->success("成功", $list);

    }





    public function tixian_list()

    {

      $map['user_id'] = $this->auth->id;



      $list = Db::table('fa_withdraw')->where($map)->order('createtime desc')->paginate(12);

      $list = $list->toArray();

      foreach ($list['data'] as $key => $value) {

         $list['data'][$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);

      }



      $this->success("成功", $list);

    }





     public function scorelist()

    {

      $map['user_id'] = $this->auth->id;



      $list = Db::table('fa_user_score_log')->where($map)->order('createtime desc')->paginate(12);

      $list = $list->toArray();

      foreach ($list['data'] as $key => $value) {

         $list['data'][$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);

      }



      $this->success("成功", $list);

    }


    public function moneylist()
    {
     $post = $this->request->post();
      $map['user_id'] = $this->auth->id;

      if ($post['money_type'] == 1) {
         $map['money'] = ['>',0];
      }else{
         $map['money'] = ['<',0];
      }

      $list = Db::table('fa_user_money_log')->where($map)->order('createtime desc')->paginate(12);

      $list = $list->toArray();

      foreach ($list['data'] as $key => $value) {

         $list['data'][$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);

      }



      $this->success("成功", $list);

    }





    public function my_user()

    {

      $map['inv_id'] = $this->auth->id;



        $list = Db::table('fa_user')->where($map)->field('id,nickname,avatar,createtime')->order('createtime desc')->paginate(12);

        



        $list = $list->toArray();



        foreach ($list['data'] as $key => $value) {

            $list['data'][$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);

             if (!empty($value['avatar'])) {

                $list['data'][$key]['avatar'] = $value['avatar'];

            } else{

                $list['data'][$key]['avatar'] = $this->request->root(true) .'/assets/img/avatar.png';  

            }

        }

      

        //我的关注  

        $this->success("成功", $list);



    

    }





     //初始化经销商信息

    public function tuiguanginit(){

        

        $map['user_id'] = $this->auth->id;

        $info = Db::table('fa_pifa_tuiguangyuan')->where($map)->find();



        if (!empty($info)) {

           

           $this->success("请求成功", $info);

        }else{

            $this->error("请求成功");

        }

    }



    //提交经销商申请

    public function edittuiguang(){

        $post = file_get_contents( "php://input");

        $post = json_decode($post,true);

        $rule = [

                'name'  => 'require',

                'mobile'  => 'require|regex:/^1\d{10}$/',

                'idcard'  => 'require',

                'address'     => 'require',

                'emo'     => 'require',

        ];

        $msg = [         

                'name.require' => '请输入联系人',

                'mobile.require' => '请输入电话',

                'idcard.require' => '请输入身份证',

                'address.require' => '请选择地址',

                'emo.require' => '请输入申请原因',

              

        ];

        $data = $post['data'];

        $validate = new Validate($rule, $msg);

        $result = $validate->check($data);

        if (!$result) {

            $this->error(__($validate->getError()));

        }

        $map['user_id'] = $this->auth->id;

        $res = Db::table('fa_pifa_tuiguangyuan')->where($map)->find();



        if (empty($res)) {

            $params['user_id'] = $this->auth->id;

            $params['name'] = $post['data']['name'];

            $params['mobile'] = $post['data']['mobile'];

            $params['idcard'] = $post['data']['idcard'];

            $params['emo'] = $post['data']['emo'];

            $params['sex'] = $post['data']['sex'];

            $params['address'] = $post['data']['address'];

            $params['province_name'] = $post['data']['province_name'];

            $params['city_name'] = $post['data']['city_name'];

            $params['area_name'] = $post['data']['area_name'];

            $params['province_id'] = $post['data']['province_id'];

            $params['city_id'] = $post['data']['city_id'];

            $params['area_id'] = $post['data']['area_id'];



          

            $params['createtime'] = time();

            $params['updatetime'] = time();





            Db::table('fa_pifa_tuiguangyuan')->insert($params);



            $this->success("提交成功");

        }else{

            $params['user_id'] = $this->auth->id;

            $params['name'] = $post['data']['name'];

            $params['mobile'] = $post['data']['mobile'];

            $params['idcard'] = $post['data']['idcard'];

            $params['emo'] = $post['data']['emo'];

            $params['sex'] = $post['data']['sex'];

            $params['address'] = $post['data']['address'];

            $params['province_name'] = $post['data']['province_name'];

            $params['city_name'] = $post['data']['city_name'];

            $params['area_name'] = $post['data']['area_name'];

            $params['province_id'] = $post['data']['province_id'];

            $params['city_id'] = $post['data']['city_id'];

            $params['area_id'] = $post['data']['area_id'];

         

            $params['updatetime'] = time();

            

            $map2['user_id'] = $this->auth->id;

            $map2['id'] = $post['data']['id'];

            Db::table('fa_pifa_tuiguangyuan')->where($map2)->update($params);



            $this->success('修改成功' );

        }



    }






    //初始化经销商信息

    public function mdinit(){

        

        $map['user_id'] = $this->auth->id;

        $info = Db::table('fa_pifa_md')->where($map)->find();



        if (!empty($info)) {


            $info['images'] = explode(',',$info['images']);
            $images = [];
            foreach ($info['images'] as $key => $value) {
                $info['images'][$key] = cdnurl($value,true);
                $images[$key]['url'] = cdnurl($value,true);
            }

            $info['fileList1'] = $images;

           $this->success("请求成功", $info);

        }else{

            $this->error("请求成功");

        }

    }



    //提交经销商申请

    public function editmd(){

        $post = file_get_contents( "php://input");

        $post = json_decode($post,true);

        $rule = [

                'name'  => 'require',

                'mobile'  => 'require|regex:/^1\d{10}$/',

                'address'     => 'require',

                // 'emo'     => 'require',

        ];

        $msg = [         

                'name.require' => '请输入联系人',

                'mobile.require' => '请输入电话',

                'address.require' => '请选择地址',

                // 'emo.require' => '请输入申请原因',

              

        ];

        $data = $post['data'];

        $validate = new Validate($rule, $msg);

        $result = $validate->check($data);

        if (!$result) {

            $this->error(__($validate->getError()));

        }



        if (empty($post['fileList1'])) {
            $this->error("请上传门店照片");
        }
        $images = [];
        foreach ($post['fileList1'] as $key => $value) {
            $images[$key] = str_ireplace($this->request->root(true), '', $value['url']);
        }
        $images = implode(',',$images);

        $params['images'] =  $images;
        $map['user_id'] = $this->auth->id;

        $res = Db::table('fa_pifa_md')->where($map)->find();
        if (empty($res)) {

            $params['user_id'] = $this->auth->id;

            $params['name'] = $post['data']['name'];

            $params['mobile'] = $post['data']['mobile'];
            $params['md_name'] = $post['data']['md_name'];
            $params['address'] = $post['data']['address'];
            $params['xiangxi'] = $post['data']['xiangxi'];
            // $params['emo'] = $post['data']['emo'];
            $params['createtime'] = time();
            $params['updatetime'] = time();

            Db::table('fa_pifa_md')->insert($params);



            $this->success("提交成功");

        }else{

            $params['user_id'] = $this->auth->id;

            $params['name'] = $post['data']['name'];

            $params['mobile'] = $post['data']['mobile'];
            $params['md_name'] = $post['data']['md_name'];

            $params['address'] = $post['data']['address'];
            $params['xiangxi'] = $post['data']['xiangxi'];
            $params['emo'] = $post['data']['emo'];

            $params['updatetime'] = time();

            

            $map2['user_id'] = $this->auth->id;

            $map2['id'] = $post['data']['id'];

            Db::table('fa_pifa_md')->where($map2)->update($params);



            $this->success('修改成功' );

        }



    }









    //初始化经销商信息

    public function dailiinit(){

        

        $map['user_id'] = $this->auth->id;

        $info = Db::table('fa_pifa_daili')->where($map)->find();



        if (!empty($info)) {

            $info['fileList1'][0]['url'] = cdnurl($info['z_image'],true);

            $info['fileList2'][0]['url'] = cdnurl($info['f_image'],true);

           $this->success("请求成功", $info);

        }else{

            $this->error("请求成功");

        }

    }



    //提交经销商申请

    public function editdaili(){

        $post = file_get_contents( "php://input");

        $post = json_decode($post,true);

        $rule = [

                'name'  => 'require',

                'mobile'  => 'require|regex:/^1\d{10}$/',

                'address'     => 'require',

                'emo'     => 'require',

        ];

        $msg = [         

                'name.require' => '请输入联系人',

                'mobile.require' => '请输入电话',

                'address.require' => '请选择地址',

                'emo.require' => '请输入申请原因',

              

        ];

        $data = $post['data'];

        $validate = new Validate($rule, $msg);

        $result = $validate->check($data);

        if (!$result) {

            $this->error(__($validate->getError()));

        }







        if (empty($post['fileList1'])) {

            $this->error("请上传身份证正面");

        }



        $params['z_image'] =  str_ireplace($this->request->root(true), '', $post['fileList1'][0]['url']);



         if (empty($post['fileList2'])) {

            $this->error("请上传身份证反面");

        }



        $params['f_image'] =  str_ireplace($this->request->root(true), '', $post['fileList2'][0]['url']);







        $map['user_id'] = $this->auth->id;

        $res = Db::table('fa_pifa_daili')->where($map)->find();



        if (empty($res)) {

            $params['user_id'] = $this->auth->id;

            $params['name'] = $post['data']['name'];

            $params['mobile'] = $post['data']['mobile'];

            $params['emo'] = $post['data']['emo'];

            $params['sex'] = $post['data']['sex'];

            $params['address'] = $post['data']['address'];

            $params['province_name'] = $post['data']['province_name'];

            $params['city_name'] = $post['data']['city_name'];

            $params['area_name'] = $post['data']['area_name'];

            $params['province_id'] = $post['data']['province_id'];

            $params['city_id'] = $post['data']['city_id'];

            $params['area_id'] = $post['data']['area_id'];



          

            $params['createtime'] = time();

            $params['updatetime'] = time();





            Db::table('fa_pifa_daili')->insert($params);



            $this->success("提交成功");

        }else{

            $params['user_id'] = $this->auth->id;

            $params['name'] = $post['data']['name'];

            $params['mobile'] = $post['data']['mobile'];

            $params['emo'] = $post['data']['emo'];

            $params['sex'] = $post['data']['sex'];

            $params['address'] = $post['data']['address'];

            $params['province_name'] = $post['data']['province_name'];

            $params['city_name'] = $post['data']['city_name'];

            $params['area_name'] = $post['data']['area_name'];

            $params['province_id'] = $post['data']['province_id'];

            $params['city_id'] = $post['data']['city_id'];

            $params['area_id'] = $post['data']['area_id'];

         

            $params['updatetime'] = time();

            

            $map2['user_id'] = $this->auth->id;

            $map2['id'] = $post['data']['id'];

            Db::table('fa_pifa_daili')->where($map2)->update($params);



            $this->success('修改成功' );

        }



    }











     //新品需求

    public function addxuqiu(){

        $post = file_get_contents( "php://input");

        $post = json_decode($post,true);

        $rule = [

                'name'  => 'require',

                'pp'  => 'require',

                'xuqiu'     => 'require',

                'price'     => 'require',

                'emo'     => 'require',

        ];

        $msg = [         

                'name.require' => '请输入商品名称',

                'pp.require' => '请输入电话',

                'xuqiu.require' => '请选择地址',

                'price.require' => '请输入申请原因',

                

              

        ];

        $data = $post['data'];

        $validate = new Validate($rule, $msg);

        $result = $validate->check($data);

        if (!$result) {

            $this->error(__($validate->getError()));

        }







        if (empty($post['fileList1'])) {

            $this->error("请上传商品图");

        } 



        $images = [];

        foreach ($post['fileList1'] as $key => $value) {

           $images[$key] = str_ireplace($this->request->root(true), '', $value['url']);

        }





        $map['user_id'] = $this->auth->id;

        $res = Db::table('fa_pifa_daili')->where($map)->find();



        $params['user_id'] = $this->auth->id;

        $params['name'] = $post['data']['name'];

        $params['pp'] = $post['data']['pp'];

        $params['xuqiu'] = $post['data']['xuqiu'];

        $params['price'] = $post['data']['price'];

        $params['images'] = '';

        if (!empty($images)) {

            $params['images'] = implode(',',$images);

        }

        

        $params['createtime'] = time();

        $params['updatetime'] = time();

        Db::table('fa_pifa_xuqiu')->insert($params);



        $this->success("提交成功");



    }





    //添加客户

    public function addkehu(){

        $post = file_get_contents( "php://input");

        $post = json_decode($post,true);

        

        $rule = [

                'consignee'  => 'require',

                'phone'  => 'require|regex:/^1\d{10}$/',

                // 'sex'     => 'require',

                'address'  => 'require',

        ];

        $msg = [         

                'consignee.require' => '请输入客户名称',

                'phone.require' => '请输入电话',

                'phone.regex' => '请输入正确的手机号',

                // 'sex.require' => '请选择性别',

                'address.require' => '请输入地址',

                

        ];

        $data = $post['data'];

        $validate = new Validate($rule, $msg);

        $result = $validate->check($data);

        if (!$result) {

            $this->error(__($validate->getError()));

        }



        if (!empty($post['fileList1'])) {

            $params['image'] =  str_ireplace($this->request->root(true), '', $post['fileList1'][0]['url']);

        }



        $map_kehu['phone'] = $post['data']['phone'];


        $kehu = Db::table('fa_pifa_kehu')->where($map_kehu)->find();    


        if (!empty($kehu)) {
            $this->error("客户已存在");
        }
    

        $map_user['mobile'] = $post['data']['phone'];


        $kehu_user = Db::table('fa_user')->where($map_user)->find();

        
        if (!empty($kehu_user)) {
            $params['user_id'] = $kehu_user['id'];
            $params['status'] = '1';
        }
        $params['xiaoshou_id'] = $this->auth->id;
        $params['consignee'] = $post['data']['consignee'];
        $params['md_name'] = $post['data']['md_name'];
        $params['phone'] = $post['data']['phone'];


        $params['address'] = $post['data']['address'];

        $params['lat'] = $post['data']['lat'];

        $params['lng'] = $post['data']['lng'];
        $params['xiangxi'] = $post['data']['xiangxi'];



          

            $params['createtime'] = time();

            $params['updatetime'] = time();



            try {

                Db::table('fa_pifa_kehu')->insert($params);

                $this->success("提交成功");

            } catch (Exception $e) {



                $this->error($e->getMessage());

            }





           

    }

    //客户详情

    public function kehudetail(){

        $post = $this->request->request();



        $info = Db::table('fa_pifa_kehu')->find($post['id']);







        $info['fileList1'][0]['url'] = cdnurl($info['image']);



        $this->success("提交成功",$info);

    }

    //我的客户

     public function my_kehu()

    {

      $map['xiaoshou_id'] = $this->auth->id;

       $Kehu = new \app\admin\model\pifa\Kehu;

        $post = $this->request->request();



       if (!empty($post['keyword'])) {

           $map['fa_pifa_kehu.consignee'] = ['like','%'.$post['keyword'].'%'];

       }





         $list = $Kehu

                    ->with(['user'])

                    ->where($map)

                    ->order('createtime desc')

                    ->paginate(12);



            foreach ($list as $row) {

                

                $row->getRelation('user')->visible(['nickname','mobile','avatar']);

            }

        $list = $list->toArray();



        foreach ($list['data'] as $key => $value) {

           $list['data'][$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);

        }





      $this->success("成功", $list);

    }



    

}

